package com.summer.bilibili.handler.type;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 颜色类型处理器
 */
@MappedJdbcTypes(JdbcType.INTEGER) //仅处理数据库中的 INTEGER类型字段。
@MappedTypes({ String.class }) //仅处理 Java 的 String类型数据
public class ColorTypeHandler extends BaseTypeHandler < String > {
    @Override
    public void setNonNullParameter ( PreparedStatement ps , int i , String parameter , JdbcType jdbcType ) throws SQLException {
        ps.setInt ( i , Integer.parseInt ( parameter.replace ( "#" , "" ) , 16 ) );
    }

    @Override
    public String getNullableResult ( ResultSet rs , String columnName ) throws SQLException {
        return "#" + Integer.toHexString ( rs.getInt ( columnName ) );
    }

    @Override
    public String getNullableResult ( ResultSet rs , int columnIndex ) throws SQLException {
        return "#" + Integer.toHexString ( rs.getInt ( columnIndex ) );
    }

    @Override
    public String getNullableResult ( CallableStatement cs , int columnIndex ) throws SQLException {
        return "#" + Integer.toHexString ( cs.getInt ( columnIndex ) );
    }
}
